<template>
  <vue-content>
    <vue-header>
      <vue-header-form>
        <el-form :inline="true" :model="formFilter" ref="formFilter">
          <el-form-item label="操作员名称" prop="userName">
            <el-input v-model.trim="formFilter.userName" style="width:150px"></el-input>
          </el-form-item>
          <el-form-item label="开始日期" prop="startTime">
				     <div class="block">
							    <el-date-picker
							      v-model.trim="formFilter.startTime"
							      format="yyyy-MM-dd HH:mm:ss"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    type="datetime"
							      placeholder="选择日期时间">
							    </el-date-picker>
							  </div>
				  </el-form-item>
		      <el-form-item label="结束日期" prop="endTime">
		         <div class="block">
						    <el-date-picker
						      v-model.trim="formFilter.endTime"
						      format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  type="datetime"
						      placeholder="选择日期时间">
						    </el-date-picker>
						  </div>
		      </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="SearchSubmit('formFilter')">查询</el-button>
            <el-button @click="resetForm('formFilter')">重置</el-button>
            <!-- <el-button @click="exportForm('formFilter')">导出</el-button> -->
          </el-form-item>
        </el-form>
      </vue-header-form>
    </vue-header>
    <vue-body>
      <el-table
      	v-loading.body="loading"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="userName"
          label="操作员名称">
        </el-table-column>
        <el-table-column
          prop="addr"
          label="ip地址">
        </el-table-column>
        <el-table-column
          prop="operaTime"
          label="操作耗时">
        </el-table-column>
        <el-table-column
          label="操作状态">
          <template scope="scope">
            {{ scope.row.state | state }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作类型">
          <template scope="scope">
            {{ scope.row.type | type }}
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="操作方法名称">
        </el-table-column>
        <el-table-column
          label="操作渠道">
          <template scope="scope">
            {{ scope.row.chl}}
          </template>
        </el-table-column>
        <el-table-column
          label="错误信息">
          <template scope="scope">
            {{ scope.row.error || "无"}}
          </template>
        </el-table-column><el-table-column
          prop="createTime"
          label="操作日期">
        </el-table-column>
      </el-table>
    </vue-body>
    <vue-footer style="padding: 5px 0;margin: 0 -5px;float:right;">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="pageSize">
      </el-pagination>
    </vue-footer>
    <!--<edit-role
      ref="edit-role"
      @close="editFinish"
      :view="view"
      v-if="view ==='create' || view ==='edit'"/>
    <edit-detail
      ref="edit-detail"
      @close="editFinish"
      :view="view"
      v-if="view ==='detail'"/>-->
  </vue-content>
</template>

<script>
  import { md5 } from '@/utilities/crypto' // 加密
  import AuthStorage from '@/services/storage.js'
  import {getLogList} from '@/services/reports'
  import {RequestError} from '@/services/errors'
  import {deepClone} from '@/utilities/object'
  import EditRole from './edit'
  import EditDetail from './Detail'
  export default {
    name: 'role-table',
    components: {EditRole,EditDetail},
    data: () => ({
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      formFilter: {
        userName:"",
        startTime:"",
        endTime:""
      },
      loading: false,
      view: '',
    }),
    methods: {
      async loadData () {
        this.loading = true
        let reg = RegExp(/T/);
        if(JSON.stringify(this.formFilter.startTime).match(reg)){
          this.formFilter.startTime = this.formatDateTime(this.formFilter.startTime);
        }
        if(JSON.stringify(this.formFilter.endTime).match(reg)){
          this.formFilter.endTime = this.formatDateTime(this.formFilter.endTime);
        }
        try {
          let data = await getLogList(Object.assign({currentPage: this.currentPage},{perPage:10},this.formFilter))
          // debugger
          this.tableData = data.sysLogs
          this.pageSize = parseInt(data.totalResult)
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      async exportData () {
        var param=Object.assign({currentPage: this.currentPage},{perPage:10},this.formFilter);
        var  a = new Date();
        var btime= a .getTime();
        var signs2 = "data="+JSON.stringify(param)+"&stmb="+btime;
        this.postDownLoadFile({
          url: this.$store.state.exportUrl + 'ecshop/console/statement/statisticsTotalAmtExport.do?stmb='+ a.getTime() +'&source="03"&token='+ AuthStorage.token +'&version=""&data='+JSON.stringify(param)+'&sign='+this.signature(signs2),
          method:'post'
        });
      },
      signature(data) {
        return md5(data)
      },
      formatDateTime (date) {  
        var y = date.getFullYear();  
        var m = date.getMonth() + 1;  
        m = m < 10 ? ('0' + m) : m;  
        var d = date.getDate();  
        d = d < 10 ? ('0' + d) : d;  
        var h = date.getHours();  
        h=h < 10 ? ('0' + h) : h;  
        var minute = date.getMinutes();  
        minute = minute < 10 ? ('0' + minute) : minute;  
        var second=date.getSeconds();  
        second=second < 10 ? ('0' + second) : second;  
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;  
      },

//    async  create (index, data) {
//      this.view = 'create'
//      await this.$nextTick()
//      this.$refs['edit-role'].orderNo = data.orderNo
//    },
//    async handleEdit (index, data) {
//      this.view = 'edit'
//      await this.$nextTick()
//      this.$refs['edit-role'].dialogFormVisible = true
//      this.$refs['edit-role'].formData = data
//    },
//    async handleDetail (index, data) {
//      this.view = 'detail'
//      await this.$nextTick()
//      this.$refs['edit-detail'].id = data.id
//      this.$refs['edit-detail'].loadData()
//    },
      SearchSubmit () {
        this.loadData()
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
        this.currentPage = 1
        this.loadData()
      },
      exportForm(){
        this.exportData();
      },
      postDownLoadFile(options) {
          var config = $.extend(true, { method: 'post' }, options);
          var $iframe = $('<iframe id="down-file-iframe" />');
          var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
          $form.attr('action', config.url);
          for (var key in config.data) {
              $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
          }
          $iframe.append($form);
          $(document.body).append($iframe);
          $form[0].submit();
          $iframe.remove();
      }
    },
    watch: {
      currentPage (newVal) {
        this.loadData(newVal)
      }
    },
    filters: {
      type (value) {
        return {'ADD': '新增','UPDATE': '修改','DEL': '删除','SEARCH': '搜索'}[value]
      },
      state (value) {
      	//操作状态
        return {'0': '成功', '1': '失败'}[value]
      }
    },
    created () {
      this.loadData()
    }
  }
</script>
